<?php

namespace App\Http\Controllers\Home;

require_once 'resources/org/code/Code.class.php';

use App\Http\Models\Member;

class RegisterController extends CommonController
{
    public function register(){
        return view('home.register');
    }
    
    public function store(){
        $input = \Input::except('_token');
        if ($input) {
            $rules = [
                'member_name' => 'required|unique:member,member_name',
                'member_nickname' => 'required',
                'member_cellphone' => 'required|size:11|unique:member,member_cellphone',
                'member_email' => 'required|email',
                'member_password' => 'required|min:6|confirmed'
            ];

            $messages = [
                'member_name.required' => '用户名称不能为空！',
                'member_name.unique' => '用户名称必须唯一！',
                'member_nickname.required' => '昵称不能为空',
                'member_cellphone.required' => '手机号不能为空！',
                'member_cellphone.size' => '手机号位数不对！',
                'member_cellphone.unique' => '该手机号已被注册！',
                'member_email.required' => '用户电子邮箱格式不能为空！',
                'member_email.email' => '用户电子邮箱格式不正确！',
                'member_password.required' => '用户密码不能为空！',
                'member_password.min' => '用户密码太短，不到6位！',
                'member_password.confirmed' => '确认密码与密码不匹配'
            ];
            
            $validator = \Validator::make($input, $rules, $messages);
            if ($validator->passes()) {
                $submit['member_name'] = $input['member_name'];
                $submit['member_nickname'] = $input['member_nickname'];
                $submit['member_cellphone'] = $input['member_cellphone'];
                $submit['member_email'] = $input['member_email'];
                $submit['member_password'] = md5($input['member_password']);
                $submit['member_createtime'] = time();
                $submit['member_updatetime'] = time();
                
                $r = Member::create($submit);
                if($r)
                    return redirect ('home/index');
                else 
                    return back()->with ('errors', '创建失败，请稍后再试！');
            } else
                return back()->withErrors($validator);
        }
    }
            
    function code(){
        $code = new \Code;
        $code->make();
    }
    
    public function clear(){
        session(['member'=> null]);
    }
    
    public function quit(){
        session(['member'=> null]);
        return redirect('admin/login');
    }
    
    
}
